<?php
namespace app\index\controller;

class Setting {
	public function index() {
		$boy_avator_list = array('/boy.png');
		$girl_avator_list = array('/girl.png');
		for ($i = 1; $i < 9; $i++) {
			$boy_avator = '/boy'.$i.'.png';
			$girl_avator = '/girl'.$i.'.png';
			array_push($boy_avator_list, $boy_avator);
			array_push($girl_avator_list, $girl_avator);
		}
		return view('index/setting', [
			'boy_avator_list' => $boy_avator_list,
			'girl_avator_list' => $girl_avator_list
		]);
	}

	public function setAvatorDefault() {
		$avator = input('post.avator');
		$user_id = input('session.user_id');

		if ($user_id == null) {
			return json([
                'status' => 'error',
                'message' => '未登录，请重试！',
                'data' =>  null
            ]);
		}

		if ($avator) {
			$cuser = model('User');
			$cuser->save([
			    'avator_path' => $avator
			],['id' => $user_id]);
			session('avator_path', $avator); 
			return json([
                'status' => 'success',
                'message' => '修改成功！',
                'data' =>  null
            ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '上传失败！',
                'data' =>  null
            ]);
		}
	}

	public function setAvatorCustomize() {
		$avator = input('post.avator');
		$user_id = input('session.user_id');
		if ($user_id == null) {
			return json([
                'status' => 'error',
                'message' => '未登录，请重试！',
                'data' =>  null
            ]);
		}

		if ($avator) {
			$if_jpeg = substr_count($avator, "image/jpeg");
			$if_png = substr_count($avator, "image/png");
			if ($if_jpeg == 0 and $if_png == 0) {
				return json([
	                'status' => 'error',
	                'message' => '图片类型不符合！',
	                'data' =>  null
	            ]);
			}

			if ($if_jpeg != 0) {
				$type = '.jpg';
			}
			if ($if_png != 0) {
				$type = '.png';
			}

			$avator_data = substr($avator, strpos($avator,",") + 1);
			$avator_decoded_data = base64_decode($avator_data);
			$save_name = '/'. date('Ymd') . '/' . md5(microtime(true)) . $type;
			$save_path = AVATOR_PATH.$save_name;
			if ($this->checkPath(dirname($save_path))) {
				file_put_contents($save_path, $avator_decoded_data);
				$cuser = model('User');
				$cuser->save([
				    'avator_path' => $save_name
				],['id' => $user_id]);
				session('avator_path', $save_name); 
				return json([
	                'status' => 'success',
	                'message' => '修改成功！',
	                'data' =>  null
	            ]);
			}
			else {
				return json([
	                'status' => 'error',
	                'message' => '上传失败！',
	                'data' =>  null
	            ]);
			}
			
		}
		else {
			return json([
                'status' => 'error',
                'message' => '上传失败，请重试！',
                'data' =>  null
            ]);
		}
		
	}

	public function setIntroduction() {
		$introduction = input('post.introduction');
		$user_id = input('session.user_id');
		if ($user_id != null and $introduction != null) {
			$cuser = model('User');
			$cuser->save([
			    'introduction' => $introduction
			],['id' => $user_id]);
			session('introduction', $introduction);
			return json([
                'status' => 'success',
                'message' => '修改成功！',
                'data' =>  null
            ]);
		}
	}

	public function setPassword() {
		$opassword = input('post.opassword');
		$npassword = input('post.npassword');
		$user_id = input('session.user_id');

		if ($user_id != null and $opassword != null and $npassword != null) {
			$cuser = model('User');
			$password_data = $cuser::where('id', $user_id)->find();
			if ($password_data != null) {
				if ($password_data->password != $opassword) {
					return json([
		                'status' => 'error',
		                'message' => '原密码填写错误！',
		                'data' =>  null
		            ]);
				}
				if ($password_data->password == $npassword) {
					return json([
		                'status' => 'error',
		                'message' => '原密码不能与新密码相同！',
		                'data' =>  null
		            ]);
				}
				$cuser->save([
			    	'password' => $npassword
				],['id' => $user_id]);
				return json([
		                'status' => 'success',
		                'message' => '修改成功！',
		                'data' =>  null
		            ]);
			}
			else {
				return json([
	                'status' => 'error',
	                'message' => '无用户信息！',
	                'data' =>  null
	            ]);
			}
		}
		else {
			return json([
	                'status' => 'error',
	                'message' => '修改错误，请重试！',
	                'data' =>  null
	            ]);
		}
	}

	private function checkPath($path) {
        if (is_dir($path) || mkdir($path, 0755, true)) {
            return true;
        }
        return false;
    }
}